(function (exports) {

    var partner;
    var subPartner;
    var info_clear = {};
    var info_agg = {};

    var firstReport = true;
    var lastReportTime = getCurrentTs();
    var uuid = generateUUID();
    var version;
    var reporting = false;
    var reportCount = 0;
    var timeIntervalList = [200, 500, 1000, 2000, 3000, 5000];


    function init(p, sp, v) {
        partner = p;
        subPartner = sp;
        version = v;
    }

    function generateUUID() {
        var d = new Date().getTime();
        if (typeof performance !== 'undefined' && typeof performance.now === 'function') {
            d += performance.now(); //use high-precision timer if available
        }
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
    }


    function start() {
        console.log('yyreporter start');
        setTimeout(report, 1000);
        setInterval(report, 1 * 60 * 1000);
        lastReportTime = getCurrentTs();
    }


    function report() {
        console.log('yyreporter report');

        //first time
        if (firstReport) {
            firstReport = false;
            add('into', 1);
        }

        try {
            if (!reporting) {
                console.log('yyreporter do report');

                // this.gatherInfo();
                // this.reporting = true;
                var reportInfo = {'uid': gameData.uid, 'code': '000', 'c': partner + "_" + subPartner, 'i': {}};
                reportInfo['uuid'] = uuid;
                reportInfo['version'] = version;


                for (var k in info_clear) {
                    reportInfo['i'][k] = info_clear[k];
                }
                for (var k in info_agg) {
                    reportInfo['i'][k] = info_agg[k];
                }

                reportInfo['i']['dl'] = getCurrentTs() - lastReportTime;
                if (reportInfo['i']['dl'] < 0) {
                    reportInfo['i']['dl'] = 50 * 1000;
                } else if (reportInfo['i']['dl'] > 1500000) {
                    reportInfo['i']['dl'] = 50 * 1000;
                }

                reportInfo['i']['report_count'] = reportCount++;


                var xhr = new XMLHttpRequest();
                // console.log('yyreport xhr' + xhr);
                xhr.open('POST', "http://dadian.yygameapi.com:6001/dadian", true);
                xhr.timeout = 2000;
                xhr.onTimeout = reportTimeOut;
                xhr.onload = reportSucc;
                xhr.send(JSON.stringify(reportInfo));
                console.log('yyreport reportInfo' + JSON.stringify(reportInfo));
                lastReportTime = getCurrentTs();
            }
        } catch (e) {
            console.log(e);
        }
    }


    function getCurrentTs() {
        return (new Date().valueOf());
    }

    function reportSucc() {
        reporting = false;
        info_clear = {};
        delete info_clear;
        info_clear = {};
    }

    function doAdd(k, v, w) {
        if (w.hasOwnProperty(k)) {
            w[k] += v;
        } else {
            w[k] = v;
        }
    }

    function add(a, b) {
        console.log("Repoter add " + a + ":" + b);
        return doAdd(a, b, info_clear);
    }

    //添加时间间隔打点
    function addStep(a, b) {
        console.log("Repoter addStep " + a + ":" + b);
        this.add(a, b);
        var c = timeIntervalList;
        if (cc.isArray(c)) {
            for (var i = 0; i < c.length; i++) {
                if (b < c[i]) {
                    this.add(a + '_' + c[i], 1);
                    break;
                }
            }

            if (c.length > 0 && b > c[c.length - 1]) {
                this.add(a + '_max', 1);
            }
        }
    }

    function addAndReport(a, b) {
        add(a, b);
        report();
    }

    function addAgg(a, b) {
        return doAdd(a, b, info_agg);
    }

    function reportTimeOut() {
        add('report_err', 1);
    }

    exports.YYReporterUtil = {
        init: init,
        start: start,
        add: add,
        addAgg: addAgg,
        addStep: addStep
    }


})(window);

